package com.car.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.car.common.Result;
import com.car.entity.RepairRecord;
import com.car.service.RepairRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 维修记录控制器
 *
 * @author Car System
 */
@Tag(name = "维修记录管理")
@RestController
@RequestMapping("/repair")
@RequiredArgsConstructor
public class RepairRecordController {

    private final RepairRecordService repairRecordService;

    @Operation(summary = "分页查询维修记录")
    @GetMapping("/page")
    public Result<IPage<RepairRecord>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        IPage<RepairRecord> page = repairRecordService.page(current, size, keyword, status, startTime, endTime);
        return Result.success(page);
    }

    @Operation(summary = "根据车辆ID查询维修记录")
    @GetMapping("/vehicle/{vehicleId}")
    public Result<List<RepairRecord>> listByVehicleId(@PathVariable Long vehicleId) {
        List<RepairRecord> list = repairRecordService.listByVehicleId(vehicleId);
        return Result.success(list);
    }

    @Operation(summary = "根据客户ID查询维修记录")
    @GetMapping("/customer/{customerId}")
    public Result<List<RepairRecord>> listByCustomerId(@PathVariable Long customerId) {
        List<RepairRecord> list = repairRecordService.listByCustomerId(customerId);
        return Result.success(list);
    }

    @Operation(summary = "根据ID查询维修记录")
    @GetMapping("/{id}")
    public Result<RepairRecord> getById(@PathVariable Long id) {
        RepairRecord record = repairRecordService.getById(id);
        return Result.success(record);
    }

    @Operation(summary = "新增维修记录")
    @PostMapping
    public Result<Void> save(@RequestBody RepairRecord record) {
        repairRecordService.save(record);
        return Result.success("新增成功");
    }

    @Operation(summary = "更新维修记录")
    @PutMapping
    public Result<Void> update(@RequestBody RepairRecord record) {
        repairRecordService.update(record);
        return Result.success("更新成功");
    }

    @Operation(summary = "更新维修状态")
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam String status) {
        repairRecordService.updateStatus(id, status);
        return Result.success("状态更新成功");
    }

    @Operation(summary = "删除维修记录")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        repairRecordService.delete(id);
        return Result.success("删除成功");
    }
}

